#ifndef SYSINFO_WINDOWS_REGKEY_H_
#define SYSINFO_WINDOWS_REGKEY_H_

#include <windows.h>
#include <string>
using namespace std;

namespace sysinfo { namespace windows {

class RegKey
{
public:
	inline static DWORD ToDWORD(const string& value) { return *(DWORD*)&value[0]; }
	inline static string FromDWORD(DWORD value) { return string((const char*)&value, sizeof(value)); }

	RegKey(HKEY key, REGSAM access_type=KEY_ALL_ACCESS);
	~RegKey();

	inline void SetAccessType(REGSAM access_type) { access_type_ = access_type; }

	bool IsValid() const;
	size_t GetKeyCount() const;
	size_t GetValueCount() const;
	inline DWORD GetError() const { return error_; }
	
	RegKey GetKey(const string& name) const;
	string GetValue(const string& name, DWORD& type) const;
    bool HasKey(const string& name) const;
    bool HasValue(const string& name) const;

	bool EnumKey(int index, string& name, string& cls) const;
	bool EnumValue(int index, string& name, DWORD& type, string& value) const;

	RegKey PutKey(const string& name, const string& cls="") const;
	bool RemoveKey(const string& name) const;
	inline bool Remove() const { return RemoveKey(""); }

	bool PutValue(const string& name, DWORD type, const string& value) const;
	bool RemoveValue(const string& name) const;

    RegKey& operator=(const RegKey& rhs);

private:
	HKEY key_;
	REGSAM access_type_;
	mutable DWORD error_;
};

}}

#endif // SYSINFO_WINDOWS_REGKEY_H_